package com.plant.tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class CalculateDate {
	private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/** 
	 * 根据当前日期获得所在周的日期区间（周一和周日日期） 
	 *  
	 * @return 
	 * @author zhaoxuepu 
	 * @throws ParseException 
	 */  
	public static String getTimefirstday(Date date) {  
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
		if (1 == dayWeek) {  
			cal.add(Calendar.DAY_OF_MONTH, -1);  
		}  
		cal.setFirstDayOfWeek(Calendar.MONDAY);  
		int day = cal.get(Calendar.DAY_OF_WEEK);  
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
		String imptimeBegin = sdf.format(cal.getTime());  
		return imptimeBegin;
	}  
	public static String getTimelastDay(Date date) {  
		Calendar cal = Calendar.getInstance();  
		cal.setTime(date);  
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天  
		if (1 == dayWeek) {  
			cal.add(Calendar.DAY_OF_MONTH, -1);  
		}  
		cal.setFirstDayOfWeek(Calendar.MONDAY);  
		int day = cal.get(Calendar.DAY_OF_WEEK);  
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);  
		cal.add(Calendar.DATE, 6);  
		String imptimeEnd = sdf.format(cal.getTime());  
		return imptimeEnd;  
	}  


	/** 
	 * 根据当前日期获得上周的日期区间（上周周一和周日日期） 
	 *  
	 * @return 
	 * @author zhaoxuepu 
	 */  
	public String getLastTimeInterval() {  
		Calendar calendar1 = Calendar.getInstance();  
		Calendar calendar2 = Calendar.getInstance();  
		int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;  
		int offset1 = 1 - dayOfWeek;  
		int offset2 = 7 - dayOfWeek;  
		calendar1.add(Calendar.DATE, offset1 - 7);  
		calendar2.add(Calendar.DATE, offset2 - 7);  
		// System.out.println(sdf.format(calendar1.getTime()));// last Monday  
		String lastBeginDate = sdf.format(calendar1.getTime());  
		// System.out.println(sdf.format(calendar2.getTime()));// last Sunday  
		String lastEndDate = sdf.format(calendar2.getTime());  
		return lastBeginDate + "," + lastEndDate;  
	}  

	@SuppressWarnings("static-access")
	public static String getmonthlasttime(){//获取当月最后一天
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.getActualMaximum(calendar.DATE));
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		String monthlastday=sf.format(calendar.getTime());
		System.out.println(monthlastday);
		return monthlastday+"23:59:59";	
	}
	@SuppressWarnings("static-access")
	public static String getmonthfirst(){//获取当月最后一天
		Calendar calendar=Calendar.getInstance();
		calendar.set(Calendar.DATE, calendar.getActualMaximum(calendar.DATE));
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH::");
		Date theDate=calendar.getTime();
		GregorianCalendar gcLast=(GregorianCalendar)Calendar.getInstance();
		gcLast.setTime(theDate);
		gcLast.set(Calendar.DAY_OF_MONTH, 1);
		String day_first=sf.format(gcLast.getTime());
		return day_first+"00:00:00";	
	}

	public static String getyesterday(){
		Calendar   cal   =   Calendar.getInstance();
		cal.add(Calendar.DATE,   -1);
		String yesterday = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(cal.getTime());
		return yesterday;
	}

	public static void main(String[] args) {
		Date date=new Date();
		System.out.println(CalculateDate.getTimefirstday(date));
		System.out.println(CalculateDate.getTimelastDay(date));

	}

}
